package com.sfh.cpc.home.fragment.view;

import android.arch.lifecycle.Lifecycle;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.R;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.widget.SpaceItemLeftBottomDecoration;
import com.sfh.cpc.home.adapter.HomeNoteAdapter;
import com.sfh.cpc.home.model.HomeViewModel;
import com.sfh.cpc.note.NoteActivity;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleFragment;

import java.util.List;

/**
 * 功能描述: VIP 话题
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class NoteVipFragmentView extends AbstractLifecycleFragment<HomeViewModel> {

    public static Fragment newFragment(int index) {
        Fragment fragment =  new NoteVipFragmentView();
        Bundle data = new Bundle();
        data.putInt("INDEX",index);
        fragment.setArguments(data);
        return fragment;
    }
    @Override
    public String getName() {
        return "VIP";
    }

    RecyclerView layout;
    HomeNoteAdapter adapter;
    ProgressBar progressBar;

    @Override
    public int getLayout() {
        return R.layout.home_note_fragment;
    }

    @Override
    public void initData(View view) {
        progressBar = view.findViewById(R.id.progressBar);
        TextView tv = view.findViewById(R.id.tv_title);
        tv.setText("专享内容 舍我其谁");
        layout = view.findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        layout.setLayoutManager(gridLayoutManager);
        layout.addItemDecoration(new SpaceItemLeftBottomDecoration(17));
      final  int index = getArguments().getInt("INDEX",0);
        adapter = new HomeNoteAdapter();
        layout.setAdapter(adapter);
        getViewModel().queryHomeVIPNote(index);


        view.findViewById(R.id.tv_change).setVisibility(index == 0?View.VISIBLE:View.GONE);
        view.findViewById(R.id.tv_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                progressBar.setVisibility(View.VISIBLE);
                handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
                getViewModel().queryHomeVIPNote(index);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ENote note = (ENote) adapter.getData().get(position);
                NoteActivity.startUI(getContext(), note);
            }
        });
    }

    @LiveDataMatch
    public void onNoteListSuccess(List<ENote> data) {
        progressBar.setVisibility(View.GONE);
        adapter.setNewData(data);
    }
}
